﻿using System;
using UnityEngine;
using UnityEngine.UI;

public class PhotosWidget : MonoBehaviour
{
    public PhotoItem icon;
    public PhotoItem tex1;
    public PhotoItem tex2;
    public PhotoItem tex3;
    public PhotoItem tex4;
    public PhotoItem tex5;
    public PhotoItem tex6;

    public GameObject editIconBtn;

    public Image[] dots;

    public ItemListDispersed ItemListDispersed;

    public Color activeDotColor = Color.white;
    public Color disactiveDotColor = Color.white;


    void Start()
    {

    }

    public int InitTextures(bool isMe,PhotosData photosData)
    {
        //GameDebug.Log($"tex1:{userInfo.tex1?.tex?.Length}  width:{userInfo.tex1?.texWidth} height:{userInfo.tex1?.texHeight}");

        editIconBtn.SetActive(isMe);

        int count = 0;

        if (InitItem(icon, photosData.icon)) count++;
        if (InitItem(tex1, photosData.tex1)) count++;
        if (InitItem(tex2, photosData.tex2)) count++;
        if (InitItem(tex3, photosData.tex3)) count++;
        if (InitItem(tex4, photosData.tex4)) count++;
        if (InitItem(tex5, photosData.tex5)) count++;
        if (InitItem(tex6, photosData.tex6)) count++;

        for(int i = 0; i < dots.Length; i++)
        {
            if (i < count)
                dots[i].gameObject.SetActive(true);
            else
                dots[i].gameObject.SetActive(false);
        }
        if (count > 0)
            ItemListDispersed.maxIndex = count - 1;

        return count;
    }

    bool InitItem(PhotoItem photoItem,string tex)
    {
        if (!string.IsNullOrEmpty(tex))// userInfo.tex6 != null && !userInfo.tex6.isNull)
        {
            photoItem.Init(tex);
            photoItem.gameObject.SetActive(true);
            return true;
        }
        else
        {
            photoItem.gameObject.SetActive(false);
            return false;
        }
    }

    private void ActiveDot(int curIndex)
    {
        for(int i = 0; i < dots.Length; i++)
        {
            if (i == curIndex)
                dots[i].color = this.activeDotColor;
            else
                dots[i].color = this.disactiveDotColor;
        }
    }

    public void OnClickEditIcon()
    {
        //EditPhotoItem.LoadImg((texture) =>
        //{
        //    OnLoadedImg(texture);
        //});

        GetComponentInParent<UserHomePanel>().ShowEditIconWidget();
    }

    public void OnLoadedImg(Texture2D texture)
    {
        if (texture != null)
        {
            Tex tex = new Tex();
            tex.texWidth = texture.width;
            tex.texHeight = texture.height;
            tex.tex = texture.EncodeToJPG(80);

            RequestServerController.instance.ReqUploadPhotos(TexId.icon, tex, (url) =>
            {
                icon.Init(url);
                GetComponentInParent<UserHomePanel>().HideEditIconWidget();
            });
        }
    }



    int m_CurIndex;
    void Update()
    {
        if(m_CurIndex != ItemListDispersed.curIndex)
        {
            int index = Mathf.Abs(ItemListDispersed.curIndex);
            ActiveDot(index);
            m_CurIndex = index;
        }
    }

}
